package com.zengtengpeng.generator.utils;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;

import com.zengtengpeng.generator.bean.AutoCodeParam;
/**
 * 生成工具
 * @author liguangcai
 * @date 2019/03/13 16:44
 */
@SuppressWarnings("serial")
public class GenUtil implements Serializable{
	
/*	private String getSql(Fault fault){
		StringBuilder sb = new StringBuilder();
		if (!"".equals(fault.getUserNum_())&&null!=fault.getUserNum_()) {
			sb.append(" and user_num = '"+fault.getUserNum_()+"'");
		}
		if (null!=fault.getType()) {
			sb.append(" and type = "+fault.getType());
		}
		if (null!=fault.getStatus()) {
			sb.append(" and status = "+fault.getStatus());
		}
		return sb.toString();
	}
	*/
	public static void genPrivoderCondition(AutoCodeParam autoCodeParam,IntrospectedTable introspectedTable){
		List<IntrospectedColumn> allColumns = introspectedTable.getAllColumns();// 获取所有的列，根据列可以获取实体属性
		List<IntrospectedColumn> collect = allColumns.stream().filter(t -> !t.isAutoIncrement()&&!"CURRENT_TIMESTAMP".equals(t.getDefaultValue())).collect(Collectors.toList());
		StringBuilder sb = new StringBuilder();
		StringBuilder updateS = new StringBuilder();
		StringBuilder resultS=  new StringBuilder("{");
		String entityName = autoCodeParam.getTableName();
		String lowerName = MyStringUtils.firstLowerCase(entityName);
		/**
		 * 生成动态判断
		 */
		for (IntrospectedColumn introspectedColumn : collect) {
			String javaProperty = introspectedColumn.getJavaProperty();
			String actualColumnName = introspectedColumn.getActualColumnName(); // 实际列名
			String upperJavaProperty = MyStringUtils.firstUpperCase(javaProperty); // 首字母大写
			String getProperty = lowerName+".get"+upperJavaProperty+"()"; // get方法
			sb.append("\tif(");
			
			if (introspectedColumn.getFullyQualifiedJavaType().getShortName().equals("String")) { // 参数是String类型
//				sb.append("!\"\".equals("+getProperty+")&&");
//				sb.append("null!="+getProperty+") {\n");
				sb.append("StringUtils.isNotBlank("+getProperty+")){\n");
				sb.append("\t\t\tsb.append(\" and ");
				sb.append(actualColumnName+" = "+"'\"+"+getProperty+"+\"'\");\n\t\t}");
				// 更新
//				sb.append(" user_name = #{userName,jdbcType=VARCHAR},");
				updateS.append("\n\t\tif(");
				updateS.append("StringUtils.isNotBlank("+getProperty+")){\n");
				updateS.append("\t\t\tsb.append(\" ");
//				updateS.append(actualColumnName+" = "+"'\"+"+getProperty+"+\"',\");\n\t\t}");
				updateS.append(""+actualColumnName+" = #{"+javaProperty+",jdbcType="+introspectedColumn.getJdbcTypeName()+"},\");\n\t\t}");
			}else {
				sb.append("null!="+getProperty+") {\n");
				sb.append("\t\t\tsb.append(\" and ");
				sb.append(actualColumnName+" = "+"\"+"+getProperty+");\n\t\t}");
				// 更新
				if (!"id".equals(actualColumnName)) {
					updateS.append("\n\t\tif(");
					updateS.append("null!="+getProperty+") {\n");
					updateS.append("\t\t\tsb.append(\" ");
//					updateS.append(actualColumnName+" = "+"\"+"+getProperty+"+\",\");\n\t\t}");
					updateS.append(""+actualColumnName+" = #{"+javaProperty+",jdbcType="+introspectedColumn.getJdbcTypeName()+"},\");\n\t\t}");
				}
			}
		}
		/**
		 * 生成insert语句
		 */
		StringBuilder insertS = new StringBuilder();
		insertS.append("insert into "+introspectedTable.getTableConfiguration().getTableName()+" (");
		//CURRENT_TIMESTAMP
		for (IntrospectedColumn introspectedColumn : collect) {
			if (!"id".equals(introspectedColumn.getActualColumnName() )) {
				insertS.append("`"+introspectedColumn.getActualColumnName() + "`,");
			}
		}
		insertS.deleteCharAt(insertS.length() - 1);
		insertS.append(") values (");

		for (IntrospectedColumn introspectedColumn : collect) {
			if (!"id".equals(introspectedColumn.getActualColumnName() )) {
				insertS.append( " #{" + introspectedColumn.getJavaProperty() + ",jdbcType="+ introspectedColumn.getJdbcTypeName() + "},");
			}
		}
		insertS.deleteCharAt(insertS.length() - 1);
		insertS.append(")");
		/**
		 * 生成resultsValue
		 * 如果mybatis开启了驼峰转化，则不生成也行
		 */
		for (IntrospectedColumn introspectedColumn : collect) {
			resultS.append("\n\t\t\t@Result(column=\""+introspectedColumn.getActualColumnName()+"\", property=\""+introspectedColumn.getJavaProperty()+"\"),");
		}
		resultS.deleteCharAt(resultS.length() - 1);
		resultS.append("\n\t}");
		autoCodeParam.setDynamicSql(new DynamicSql(insertS.toString(), updateS.toString(), sb.toString(),resultS.toString()));
	}

}
